package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.BBB;
import com.woniuxy.carrental.entity.CarOrder;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.service.CarOrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xilei
 * @create 2021-10-12 下午 4:03
 */
@RestController
public class ExpendController {
    @Resource
    CarOrderService carOrderService;

    @GetMapping("/expendlist")
    public ResponseResult findallexpend(){
        List<CarOrder> expend = carOrderService.findallDateExpend();
        Map<Date, Double> idNameMap = getIdNameMap(expend);
        System.out.println(idNameMap);
        ArrayList<BBB> list = new ArrayList<>();
        for(Map.Entry<Date, Double> e:idNameMap.entrySet()){
            BBB bbb = new BBB(e.getKey(), e.getValue());
            list.add(bbb);
        }
        System.out.println(list);
        return new ResponseResult(list, 200, "OK");
    }

    public Map<Date, Double> getIdNameMap(List<CarOrder> carOrder){
        return carOrder.stream().collect(Collectors.toMap(CarOrder::getPick_time, CarOrder::getPay_money,(entity1, entity2) -> entity1+entity2));
    }
}
